/* **************************************************************************************** */
/* TABLE 5: GROWTH OF EXPORTS DECOMPOSITION: EXTENSIVE, INTENSIVE (SURVIVAL AND DEEPENINNG) */
/* **************************************************************************************** */


drop _all
set mem 500000k

/* ************** */
cd file_aux
/* ************** */

/*Sample SPANISH REGIONS */
/*Counterfactual country: AVERAGE SPANISH REGIONS */
/*Benchmark case: Hazard rates only by year of service; also deepening and entry are not industry+year differentiated*/

local year_c "1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005"

foreach yy of local year_c{

local cou_iso3 "AND ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"

set more off

/*Calculate Equation 2's components*/
/*Calculate 3 countries' hazard rates (as in program_table3.do)*/

foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'

/*drop if leftcensored==1*/
ge year0=year-1
stset year, f(failure) id(spell) origin(begin) time0(year0)

sts gen surv=s
label var surv "% surviving rel at time t"
sts gen haz=h
label var haz "exit risk conditionated to be until t"

sort expseq
keep if expseq!=expseq[_n-1]
keep reporter_iso3 expseq surv haz
replace surv=0 if surv==.
replace haz=0 if haz==.
sort reporter_iso3 expseq

save aux_table5_sh_`zz', replace
}


/*Calculate 3 countries' number of relationships per year*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen n=count(value), by(year expseq)
keep reporter_iso3 year expseq n
duplicates drop reporter_iso3 year expseq, force
sort reporter_iso3 expseq
save aux_table5_n_`zz', replace
}

/*Calculate 3 countries' exports' average value per year*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen v=mean(value), by(year expseq)
keep reporter_iso3 year expseq v
duplicates drop reporter_iso3 year expseq, force
sort reporter_iso3 year expseq
save aux_table5_v_`zz', replace
}


/*Calculate 3 countries' deepening rates*/
foreach zz of local cou_iso3{
u cou_`zz'
scalar k=`yy'+1
drop if year>k
drop if year<=`yy'
keep if expseq>1

egen v2=mean(value), by(year expseq)
keep reporter_iso3 year expseq v2
duplicates drop year expseq, force
replace expseq=expseq-1
replace year=year-1
sort reporter_iso3 year expseq

merge reporter_iso3 year expseq using aux_table5_v_`zz'
keep if _merge==3
drop _merge
ge d=v2-v
drop v
sort reporter_iso3 year expseq
save aux_table5_d_`zz', replace
}

/*Calculate 3 countries' total exports at year t*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen tx_t=total(value), by(year)
keep reporter_iso3 year expseq tx_t
duplicates drop reporter_iso3 year, force
sort reporter_iso3 year
save aux_table5_txt_`zz', replace
}

/*Calculate 3 countries' total exports at year t+1*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
drop if year>k
egen tx_t1=total(value), by(year)
drop if year<=`yy'
replace year=year-1
duplicates drop year, force
keep reporter_iso3 year tx_t1
sort reporter_iso3 year
save aux_table5_txt1_`zz', replace
}

/*Calculate 3 countries' entry rate and new entries' value*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
drop if year>k
drop if year<=`yy'
keep if expseq==1
egen e=count(value), by(year)
egen ve=mean(value), by (year)
keep reporter_iso3 year e ve
duplicates drop reporter_iso3 year, force
replace year=year-1
sort reporter_iso3 year
save aux_table5_e_`zz', replace
}


/*Construction of the matrix for the counterfactual calculations*/
foreach zz of local cou_iso3{
use aux_table5_n_`zz'
sort reporter_iso3 expseq
merge reporter_iso3 expseq using aux_table5_sh_`zz'
keep if _merge==3
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year expseq using aux_table5_d_`zz'
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year expseq using aux_table5_v_`zz'
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year using aux_table5_txt_`zz'
drop _merge


sort reporter_iso3 year 
merge reporter_iso3 year using aux_table5_txt1_`zz'
drop _merge


sort reporter_iso3 year
merge reporter_iso3 year using aux_table5_e_`zz'
drop _merge
						/* *************************************** */
drop if year==2006  				/* CHOOSE LAST YEAR OF THE ANALYSED PERIOD */
save aux_table5_`zz', replace			/* *************************************** */
}


/* ******************************** */
/* construction of counterfactual   */
/* AVERAGE OF 17 REGIONS 	      */
/* ******************************** */

use aux_table5_sh_AND
local cou_rest "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"
foreach zz of local cou_rest{
append using aux_table5_sh_`zz'
}
sort expseq reporter
collapse (mean) surv haz ,by(expseq)
ge str3 reporter_iso3="AVG"
order reporter_iso3 expseq surv haz
sort expseq
save aux_table5_h_AVG, replace




use aux_table5_d_AND
local cou_rest "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"
foreach zz of local cou_rest{
append using aux_table5_d_`zz'
}
sort year expseq reporter
collapse (mean) v2 d ,by(year expseq)
ge str3 reporter_iso3="AVG"
order reporter_iso3 year expseq v2 d
sort year expseq
save aux_table5_d_AVG, replace




use aux_table5_e_AND
local cou_rest "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"
foreach zz of local cou_rest{
append using aux_table5_e_`zz'
}
sort year reporter
collapse (mean) e ve ,by(year)
ge str3 reporter_iso3="AVG"
order reporter_iso3 year e ve
sort year
save aux_table5_e_AVG, replace


/* ***************************************************** */
/* ***************************************************** */
/* Using the counterfactual data 				   */
/* Counterfactual = average of 17 regions 		   */
/* ***************************************************** */
/* ***************************************************** */


drop _all
local cou_rest "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"
use aux_table5_AND
foreach zz of local cou_rest{
append using aux_table5_`zz'
}

rename haz haz_country
rename d d_country
rename e e_country

sort expseq
merge expseq using aux_table5_h_AVG
ge haz_counter=.
replace haz_counter=haz if _merge==3
replace haz_counter=haz_country if _merge==1

drop surv
drop if _merge==2
drop _merge

sort year expseq
merge year expseq using aux_table5_d_AVG
ge d_counter=.
replace d_counter=d if _merge==3
replace d_counter=d_country if _merge==1
drop d
drop if _merge==2
drop _merge

sort year
rename ve ve_country
merge year using aux_table5_e_AVG
drop if _merge==2
ge e_counter=.
replace e_counter=e if _merge==3
replace e_counter=e_country if _merge==1
drop e ve
rename ve_country ve

drop _merge

replace haz_country=0 if haz_country==.
replace haz_counter=0 if haz_counter==.
replace n=0 if n==.
replace d_country=0 if d_country==.
replace d_counter=0 if d_counter==.
replace v=0 if v==.
replace e_country=0 if e_country==.
replace e_counter=0 if e_counter==.
replace ve=0 if ve==.
replace tx_t=0 if tx_t==.
replace tx_t1=0 if tx_t1==.


ge intensive0=((1-haz_country)*n*d_country)-(haz_country*n*v)
egen intensive1=total(intensive0), by(reporter_iso3 year)

ge h_intensive0=((1-haz_counter)*n*d_country)-(haz_counter*n*v)
egen h_intensive1=total(h_intensive0), by(reporter_iso3 year)

ge d_intensive0=((1-haz_country)*n*d_counter)-(haz_country*n*v)
egen d_intensive1=total(d_intensive0), by(reporter_iso3 year)

save en_curso, replace

duplicates drop reporter_iso3 year, force


ge chi=h_intensive1+(e_country*ve)
ge cdi=d_intensive1+(e_country*ve)
ge cei=intensive1+(e_counter*ve)
ge actual_increase=tx_t1-tx_t
ge actual_increase2=intensive1+(e_country*ve)


keep reporter_iso3 year chi cdi cei actual_increase actual_increase2 tx_t


save en_curso_`yy', replace
}

local year_c0 "1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005"
use en_curso_1988
foreach yy of local year_c0{
append using en_curso_`yy'
}


egen t_chi=total(chi), by(reporter_iso3)
egen t_cdi=total(cdi), by(reporter_iso3)
egen t_cei=total(cei), by(reporter_iso3)
egen t_actual_increase=total(actual_increase), by(reporter_iso3)
egen t_actual_increase2=total(actual_increase2), by (reporter_iso3)

keep if year==1988
ge actual_a_a_g=(((tx_t+t_actual_increase)/tx_t)^(1/18))-1
ge c_h_a_a_g=(((tx_t+t_chi)/tx_t)^(1/18))-1
ge c_d_a_a_g=(((tx_t+t_cdi)/tx_t)^(1/18))-1
ge c_e_a_a_g=(((tx_t+t_cei)/tx_t)^(1/18))-1

ge d_h=(c_h_a_a_g-actual_a_a_g)*100
ge d_d=(c_d_a_a_g-actual_a_a_g)*100
ge d_e=(c_e_a_a_g-actual_a_a_g)*100
replace actual_a_a_g=actual_a_a_g*100

keep reporter_iso3 actual_a_a_g d_h d_d d_e

cd ..
log using res_table5.text, text replace
/****************************************************************/
/*           Counterfactual Analysis                            */
l,clean
/****************************************************************/
log close


